package jmars.mobile.worker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import jmars.mobile.common.Investigator;
import jmars.mobile.datainvestigation.Summary;

public class TestWorker implements Investigator {

	@Override
	public Object compute(File[] files, FileWriter writer) throws Exception {
		Map<String,Long> results=new HashMap<String,Long>();
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
		String row;
		String[] cols;
		FileInputStream fis;
		Date beginDate;
		String bds;
		BufferedReader reader;
		int filesFinished=0;
		int line=0;
		for(File inputFile:files){
			filesFinished++;
			line=0;
			fis = new FileInputStream(inputFile);
			reader = new BufferedReader(new InputStreamReader(fis));
			while((row=reader.readLine())!=null){
				line++;
				cols=row.split(Summary.columnSeperator);
				beginDate = Summary.parseBeginDate(cols);
				if(beginDate!=null){
					bds=sdf.format(beginDate);
					if(results.containsKey(bds))
						results.put(bds, results.get(bds)+1);
					else
						results.put(bds, 1L);
				}
			}
			fis.close();
			System.out.println(filesFinished+": "+inputFile.getName()+" finished at "+line);
		}
		for(String d:results.keySet()){
			writer.write(d+": "+results.get(d)+"\r\n");
		}
		return null;
	}
}